package com.swiftops.basic.init;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class StartInit implements ApplicationRunner {

    @Value("${server.port}")
    String serverPort;
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    ResourceLoader resourceLoader;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("--- 系统启动成功 ---");

//        RequestMappingHandlerMapping mapping = applicationContext.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
//        Map<RequestMappingInfo, HandlerMethod> handlerMethods = mapping.getHandlerMethods();
//
//        List<String> list = new ArrayList<>();
//        for (RequestMappingInfo info : handlerMethods.keySet()) {
//            Set<String> urlSet = info.getPatternsCondition().getPatterns();
//            for (String s : urlSet) {
//                list.add(String.format("127.0.0.1:%s%s", serverPort, s));
//            }
//        }
//        System.out.println(StringUtils.join(list, "\n"));
        System.out.println("redisTemplate=" + redisTemplate.hashCode());
    }
}
